package com.gas.project.gas.practice.controller;

import java.util.List;

import com.gas.common.utils.DateUtils;
import com.gas.common.utils.StringUtils;
import com.gas.common.utils.poi.ExcelUtil;
import com.gas.framework.aspectj.lang.annotation.Log;
import com.gas.framework.aspectj.lang.enums.BusinessType;
import com.gas.framework.web.controller.BaseController;
import com.gas.framework.web.domain.AjaxResult;
import com.gas.framework.web.page.TableDataInfo;
import com.gas.project.gas.grade.domain.CourseValidGrade;
import com.gas.project.gas.message.domain.Message;
import com.gas.project.gas.message.service.IMessageService;
import com.gas.project.gas.morality.domain.Morality;
import com.gas.project.system.user.domain.User;
import com.gas.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.aspectj.bridge.IMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gas.project.gas.practice.domain.Practice;
import com.gas.project.gas.practice.service.IPracticeService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 社会实践Controller
 *
 * @author xyl
 * @date 2022-04-04
 */
@Controller
@RequestMapping("/gas/practice")
public class PracticeController extends BaseController {
    private String prefix = "gas/practice";

    @Autowired
    private IPracticeService practiceService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IMessageService messageService;

    @RequiresPermissions("gas:practice:view")
    @GetMapping()
    public String practice() {
        return prefix + "/practice";
    }

    /**
     * 查询社会实践列表
     */
    @RequiresPermissions("gas:practice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Practice practice) {
        User sysUser = getSysUser();
        if(sysUser.isStudent()){
            practice.setStudentNumber(sysUser.getLoginName());
        }
        if(sysUser.isTeacher()){
            practice.setMyStudent(sysUser.getLoginName());
        }
        startPage();
        List<Practice> list = practiceService.selectPracticeList(practice);
        for (Practice p : list) {
            User user = userService.selectUserByLoginName(p.getTeacherNumber());
            Message message = new Message();
            message.setModuleName("practice");
            message.setCourseNumber(p.getPracticeId() + "");
            message.setSubmitId(sysUser.getUserId());
            message.setReceiveId(user.getUserId());
            List<Message> messages = messageService.selectMessageList(message);
            p.setReply(messages.size() > 0);
        }
        return getDataTable(list);
    }

    /**
     * 导出社会实践列表
     */
    @RequiresPermissions("gas:practice:export")
    @Log(title = "社会实践", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Practice practice) {
        List<Practice> list = practiceService.selectPracticeList(practice);
        ExcelUtil<Practice> util = new ExcelUtil<Practice>(Practice.class);
        return util.exportExcel(list, "practice");
    }

    /**
     * 新增社会实践
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存社会实践
     */
    @RequiresPermissions("gas:practice:add")
    @Log(title = "社会实践", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Practice practice) {
        return toAjax(practiceService.insertPractice(practice));
    }

    /**
     * 修改社会实践
     */
    @GetMapping("/edit/{practiceId}")
    public String edit(@PathVariable("practiceId") Long practiceId, ModelMap mmap) {
        Practice practice = practiceService.selectPracticeById(practiceId);
        mmap.put("practice", practice);
        return prefix + "/edit";
    }

    /**
     * 修改保存社会实践
     */
    @RequiresPermissions("gas:practice:edit")
    @Log(title = "社会实践", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Practice practice) {
        return toAjax(practiceService.updatePractice(practice));
    }

    /**
     * 删除社会实践
     */
    @RequiresPermissions("gas:practice:remove")
    @Log(title = "社会实践", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(practiceService.deletePracticeByIds(ids));
    }

    @RequiresPermissions("gas:practice:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<Practice> util = new ExcelUtil<>(Practice.class);
        return util.importTemplateExcel("社会实践");
    }


    @Log(title = "社会实践" , businessType = BusinessType.IMPORT)
    @RequiresPermissions("gas:practice:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Practice> util = new ExcelUtil<>(Practice.class);
        List<Practice> practiceList = util.importExcel(file.getInputStream());
        //数据校验
        for (Practice practice : practiceList) {
            practice.setCreateBy(getLoginName());
            practice.setCreateTime(DateUtils.getNowDate());
            String check = practice.check(practice);
            if(StringUtils.isNotEmpty(check)){
                return error(check);
            }
        }
        String message = practiceService.importPractice(practiceList);
        return AjaxResult.success(message);
    }
}
